package cn.jhc;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.Authenticator.RequestorType;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebFilter("/admin/*")
public class SecurityFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        // TODO Auto-generated method stub
        
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) request;
        String username = (String) req.getSession().getAttribute("logining_user");
        if(username != null && username.length() > 0) {
            chain.doFilter(request, response);
        }else {
            HttpServletResponse resp =  (HttpServletResponse) response;
            PrintWriter out = response.getWriter();
            resp.sendRedirect("/login.html?mes=securit_check_failed");

        }

    }

    @Override
    public void destroy() {
        // TODO Auto-generated method stub
        
    }
    
}
